package com.itboye.textileexpres.top;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.itboye.textileexpes.beans.DetailBean;
import com.itboye.textileexpes.beans.YoufeiBean;
import com.itboye.textileexpes.presenter.ShopCartPresenter;
import com.itboye.textileexpes.presenter.YouFeiPresenter;
import com.itboye.textileexpres.R;
import com.itboye.textileexpres.alipaytest.ActivityPaymentMethod;
import com.itboye.textileexpres.base.BaseActivity;
import com.itboye.textileexpres.base.ResultEntity;
import com.itboye.textileexpres.myset.ActivityAddressManage;
import com.itboye.textileexpres.util.IsMobileNumPhone;
import com.itboye.textileexpres.util.XImageLoader;
import com.itboye.textileexptes.config.GlobalConfig;

public class ActivityYiXiangDingdan extends BaseActivity implements Observer {
	TextView title;
	ImageView back;
	ImageView img_other;
	LinearLayout relcontain;
	LinearLayout address_lin;
	TextView shouhuo_name, faburen, tvcandi;
	ImageView goods_img;
	TextView num_tv, total_price, tvprice;
	EditText liuyan;
	String addressID;
	String name;
	String detailInfo;
	EditText call_way;
	String youfei;
	TextView ok;
	ShopCartPresenter cartPresenter;
	StringBuffer pids;

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.activity_yixiangdingdan;
	}

	@Override
	public void onMyCreate() {
		// TODO Auto-generated method stub
		title.setText("确认订单");
		cartPresenter = new ShopCartPresenter(this);
		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		pids = new StringBuffer();
		DetailBean detailBean = (DetailBean) getIntent().getSerializableExtra(
				"detail");
		for (int i = 0; i < detailBean.getCode().size(); i++) {
			View view = LayoutInflater.from(ActivityYiXiangDingdan.this)
					.inflate(R.layout.item_detail_queren, null);
			TextView goods_name1_tv = (TextView) view
					.findViewById(R.id.goods_name1_tv);
			TextView goods_name2_tv = (TextView) view
					.findViewById(R.id.goods_name2_tv);
			TextView goods_detail_tv = (TextView) view
					.findViewById(R.id.goods_detail_tv);
			TextView color_tv = (TextView) view.findViewById(R.id.color_tv);
			TextView price_tv = (TextView) view.findViewById(R.id.price_tv);
			TextView price_unit_tv = (TextView) view
					.findViewById(R.id.price_unit_tv);
			TextView num_tv = (TextView) view.findViewById(R.id.num_tv);
			ImageView goods_img = (ImageView) view.findViewById(R.id.goods_img);
			goods_name1_tv.setText("" + detailBean.getCode().get(i));
			goods_name2_tv.setText(detailBean.getGoodsname());
			goods_detail_tv.setText(detailBean.getSecondline());
			color_tv.setText(detailBean.getDesc().get(i));
			price_tv.setText(detailBean.getSingle_price().get(i));
			num_tv.setText(detailBean.getCount().get(i));
			XImageLoader.load(GlobalConfig.IMAGE_DOUBLE
					+ detailBean.getImg().get(i), goods_img);
			pids.append("," + detailBean.getId());
			relcontain.addView(view);
		}
		faburen.setText(detailBean.getName());
		tvcandi.setText(detailBean.getChandi());
		ok.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				cartPresenter.YiXiangDingDan(IsMobileNumPhone.isUid(),
						pids.substring(1, pids.length()), call_way.getText()
								+ "", liuyan.getText() + "");
			}
		});
	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == ShopCartPresenter.YixiangOrderSuccessEvent) {
				toast("" + entity.getData());
				finish();

			} else if (entity.getEventType() == ShopCartPresenter.YixiangOrderFailureEvent) {
				toast("" + entity.getData());
			}
		}

	}

}
